// pages/myChat/chatWindow/chatWindow.js
let app = getApp();
let util = require('../../../utils/util');
let API=require('../../../api.js').API;
let req=require('../../../utils/request').Request;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollHeight: 0,
    scrollTop: 0,
    destinationUser: null,
    input: '',
    messages: []
  },
  /**
   * SendMessage
   * @param {*} options 
   */
  sendMessage: function (data) {
    //console.log(data);
    let that = this;
    let message = data.detail.value.message;
    let socketTask = app.globalData.socketTask;
    let msg = {
      destinationId: that.data.destinationUser.id,
      sourceId: app.globalData.userInfo.id,
      message: message
    }
    socketTask.send({
      data: JSON.stringify(msg),
      success: function (res) {
        console.log(res);
        //更新聊天框
        let messages = that.data.messages;
        messages.push({
          message: message,
          who: 0
        });

        let top = (that.data.messages.length + 1) * 32;
        that.setData({
          input: '',
          messages: messages,
          scrollTop: top
        });

      },
      fail: function (res) {
        console.log(res);
      }
    });

    //每当发送一条信息确认对方是否在线
    app.globalData.chatPeople.forEach(function(item){
      if(item.id==that.data.destinationUser.id){
        that.data.destinationUser.online=item.online
        that.setData({
          destinationUser:that.data.destinationUser
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let height = util.windowHeight(270);
    this.setData({
      scrollHeight: height
    });

    //
    //监听正在聊天的信息
    //
    app.globalData.socketTask.onMessage(function (res) {
      let data = res.data;
      if (app.globalData.chatId == data.sourceId) {
        console.log(data);
        let messages = that.data.messages;
        let msg = JSON.parse(res.data);
        messages.push({
          message: msg.message,
          who: msg.who,
          time: msg.time
        });
        let top = (that.data.messages.length + 1) * 32;
        that.setData({
          messages: messages,
          scrollTop: top
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    let top = (this.data.messages.length + 1) * 32;
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.on('sendDestinationId', function (data) {
      that.setData({
        destinationUser: data.destinationUser,
        scrollTop: top
      });
    });
    app.globalData.chatId = this.data.destinationUser.id;

    //改变未读状态
    app.globalData.chatPeople.forEach(function(item){
      if(that.data.destinationUser.id==item.id){
        item.offlineMessage=false;
      }
    });

    //获取聊天记录
    req.get(API.getChatRecord+'?token='+app.globalData.token+'&destinationId='+app.globalData.chatId)
    .then(function(res){
      console.log(res);
      that.setData({messages:res.dataMap.messages});
    }).catch(function(res){
      console.log(res);
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    app.globalData.chatId = null;
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})